//
//  QDPayHeaderView.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/28.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol QDPayHeaderViewDelegate: class {

    /// 点击获得的响应
    func payHeaderView(view: QDPayHeaderView,type:QDPayHeaderView.PayHeaderAction)
}


extension QDPayHeaderViewDelegate {
    func payHeaderView(view: QDPayHeaderView,type:QDPayHeaderView.PayHeaderAction) {}
}

/// 支付管理的headerView
final class QDPayHeaderView: UIView {
    
    /// 支付header类型
    enum PayHeaderAction {
        /// 新的支付类型
        case new
        /// 选择支付类型
        case `switch`
    }
    
    /// 代理对象
    weak var delegate: QDPayHeaderViewDelegate?
    /// 支付管理的大标题headerView
    let largeTableHeaderView = QDLargeTitleTableHeaderView()
    /// 背景的ImageView
    let backImageView = UIImageView()
    
    //绿色背景下
    
    /// 显示 默认方式的大标题
    let titleLabel = UILabel()
    /// 显示 支付方式的标题
    let typeLabel = UILabel()
    /// 用户Id
    let userLabel = UILabel()
    
    
    // 白色背景下
    let newTypeControl = UIControl()
    /// 显示绑定新支付方式
    let newTitleLabel = UILabel()
    /// 显示新支付的优惠
    let newExternLabel = RITLExternLabel()
    /// 右箭头
    let newArrowImageView = UIImageView()
    
    /// 分割线
    let lineView = UIView()
    
    /// 顶部的响应
    let headerControl = UIControl()
    
    /// 切换支付方式
    let switchControl = UIControl()
    /// 显示切换支付方式的标题
    let switchTitleLabel = UILabel()

    /// 显示切换支付方式优惠
    let switchExternLabel = RITLExternLabel()
    /// 右箭头
    let switchArrowImageView = UIImageView()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backImageView.image = "pm_background".namedImage/*.withAlignmentRectInsets(UIEdgeInsets(top: 108, left: 20, bottom: 10, right: 20))*/
        backImageView.clipsToBounds = true
        backImageView.isUserInteractionEnabled = true
        
        titleLabel.font = Font.semibold.font(size: 20)
        titleLabel.text = "当前默认支付方式"
        titleLabel.textColor = .white
        
        typeLabel.font = Font.light.font(size: 14)
        typeLabel.textColor = .white
        typeLabel.text = "支付宝支付"
        
        userLabel.font = Font.light.font(size: 10)
        userLabel.textColor = .white
        userLabel.text = "用户ID:117871897187"
        
        newTypeControl.backgroundColor = .clear
        switchControl.backgroundColor = .clear
        headerControl.backgroundColor = .clear
        
        newTypeControl.addTarget(self, action: #selector(controlDidClick(sender:)), for:.touchUpInside)
        switchControl.addTarget(self, action: #selector(controlDidClick(sender:)), for: .touchUpInside)
        headerControl.addTarget(self, action: #selector(controlDidClick(sender:)), for: .touchUpInside)
        
        lineView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        
        newTitleLabel.text = "绑定新支付方式"
        newTitleLabel.font = Font.medium.font(size: 16)
        newTitleLabel.textColor = QDColors.black
        
        newExternLabel.textColor = QDColors.orange
        newExternLabel.font = Font.light.font(size: 11)
        newExternLabel.layer.cornerRadius = 12
        newExternLabel.textAlignment = .center
        newExternLabel.extensionSpace = 6
        newExternLabel.layer.borderColor = QDColors.orange.cgColor
        newExternLabel.layer.borderWidth = 0.5
        newExternLabel.text = ""
        
        newArrowImageView.image = "pm_arrow_right".namedImage
        
        switchTitleLabel.text = "切换支付方式"
        switchTitleLabel.font = Font.medium.font(size: 16)
        switchTitleLabel.textColor = QDColors.black
        
        switchExternLabel.textColor = #colorLiteral(red: 0.9882352941, green: 0.6117647059, blue: 0, alpha: 1)
        switchExternLabel.textAlignment = .center
        switchExternLabel.font = Font.light.font(size: 11)
        switchExternLabel.layer.cornerRadius = 12
        switchExternLabel.extensionSpace = 6
        switchExternLabel.layer.borderColor = #colorLiteral(red: 0.9882352941, green: 0.6117647059, blue: 0, alpha: 1).cgColor
        switchExternLabel.layer.borderWidth = 0.5
        switchExternLabel.text = ""
        
        switchArrowImageView.image = "pm_arrow_right".namedImage
        
        addSubview(largeTableHeaderView)
        addSubview(backImageView)
        
        backImageView.addSubview(titleLabel)
        backImageView.addSubview(typeLabel)
        backImageView.addSubview(userLabel)
        backImageView.addSubview(headerControl)
        backImageView.addSubview(newTypeControl)
        backImageView.addSubview(switchControl)
        backImageView.addSubview(lineView)
        
        newTypeControl.addSubview(newTitleLabel)
        newTypeControl.addSubview(newExternLabel)
        newTypeControl.addSubview(newArrowImageView)
        
        switchControl.addSubview(switchTitleLabel)
        switchControl.addSubview(switchExternLabel)
        switchControl.addSubview(switchArrowImageView)
        
        let topSpace = hasPhoneHeader() ? 20 : 0
        
        largeTableHeaderView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(QDLargeTitleTableHeaderViewNormalHeight)
        }
        
        backImageView.snp.makeConstraints { (make) in
            make.top.equalTo(largeTableHeaderView.snp.bottom).offset(topSpace)
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().inset(13).priority(.high)
            make.bottom.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(27)
            make.top.equalToSuperview().offset(23)
            make.height.equalTo(28)
        }
        
        typeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(27)
            make.height.equalTo(20)
            make.top.equalTo(titleLabel.snp.bottom)
        }
        
        userLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(27)
            make.top.equalToSuperview().offset(71)
            make.height.equalTo(14)
        }
        
        headerControl.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(7)
            make.right.equalToSuperview().inset(7)
            make.bottom.equalTo(newTypeControl.snp.top)
        }
        
        newTypeControl.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(7)
            make.right.equalToSuperview().inset(7)
            make.height.equalTo(63)
            make.top.equalToSuperview().offset(100)
        }
        
        switchControl.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(7)
            make.right.equalToSuperview().inset(7)
            make.height.equalTo(60)
            make.top.equalTo(newTypeControl.snp.bottom).offset(0.5)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(163)
            make.height.equalTo(0.5)
            make.left.equalToSuperview().offset(27)
            make.right.equalToSuperview().inset(27)
        }
        
        /// new
        newTitleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.height.equalTo(22)
        }
        
        
        newArrowImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.right.equalToSuperview().inset(23)
            make.centerY.equalToSuperview()
        }
        
        newExternLabel.snp.makeConstraints { (make) in
            make.height.equalTo(24)
            make.right.equalToSuperview().inset(43)
            make.centerY.equalToSuperview()
//            make.left.lessThanOrEqualTo(newTitleLabel.snp.right).offset(8).priority(.medium)
        }
        
        /// switch
        switchTitleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.height.equalTo(22)
        }
        
        switchArrowImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.right.equalToSuperview().inset(23)
            make.centerY.equalToSuperview()
        }
        
        switchExternLabel.snp.makeConstraints { (make) in
            make.height.equalTo(24)
            make.right.equalToSuperview().inset(43)
            make.centerY.equalToSuperview()
//            make.left.lessThanOrEqualTo(switchTitleLabel.snp.right).offset(8).priority(.medium)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// control点击
    @objc func controlDidClick(sender: UIControl){
        
        delegate?.payHeaderView(view: self, type: sender == switchControl ? .`switch`: .new )
    }
}
